package com.example.lib5.binaryTree.test;

/**
 * 创建日期：2022/6/3 0003 12:58
 *
 * @author tony.sun
 * 类说明：
 */
public class BinaryDemoD {
    public static void main(String[] args) {
        BinaryD binaryD = new BinaryD();
        RoleD root = new RoleD(1,"唐三");
        RoleD role2 = new RoleD(2,"小舞");
        RoleD role3 = new RoleD(3,"千仞雪");
        RoleD role4 = new RoleD(4,"千寻疾");
        RoleD role5 = new RoleD(5,"比比东");
        root.setLeft(role2);
        root.setRight(role3);
        role3.setRight(role4);
        role3.setLeft(role5);
        binaryD.setRoot(root);
        System.out.println("前中后遍历");
        System.out.println("前");
        binaryD.preOrder();
        System.out.println("中");
        binaryD.infixOrder();
        System.out.println("后");
        binaryD.postOrder();
        System.out.println("节点查找---------------------------------");
        RoleD roleD = binaryD.preSearch(5);
        if (roleD!=null) {
            System.out.println(roleD.toString());
        }else {
            System.out.println("节点为空");
        }
        RoleD roleD1 = binaryD.infixSearch(5);
        if (roleD!=null) {
            System.out.println(roleD1.toString());
        }else {
            System.out.println("节点为空");
        }
        RoleD roleD2 = binaryD.postSearch(5);
        if (roleD!=null) {
            System.out.println(roleD2.toString());
        }else {
            System.out.println("节点为空");
        }

        System.out.println("节点删除---------------------------------");
        binaryD.delete(5);
        System.out.println("删除后……");
        binaryD.preOrder();
    }
}
//节点
class RoleD{

    private String name;
    private int no;
    private RoleD left;
    private RoleD right;

    public RoleD(int no, String name) {
        this.no=no;
        this.name=name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public RoleD getLeft() {
        return left;
    }

    public void setLeft(RoleD left) {
        this.left = left;
    }

    public RoleD getRight() {
        return right;
    }

    public void setRight(RoleD right) {
        this.right = right;
    }

    @Override
    public String toString() {
        return "RoleD{" +
                "name='" + name + '\'' +
                ", no=" + no +
                '}';
    }

    public void preOrder() {
        System.out.println(this);
        if (this.left!=null) {
            this.left.preOrder();
        }
        if (this.right!=null){
            this.right.preOrder();
        }
    }
    public void infixOrder() {

        if (this.left!=null){
            this.left.infixOrder();
        }
        System.out.println(this);
        if (this.right!=null){
            this.right.infixOrder();
        }
    }
    public void postOrder() {

        if (this.left!=null){
            this.left.postOrder();
        }
        if (this.right!=null){
            this.right.postOrder();
        }
        System.out.println(this);
    }

    public RoleD preSearch(int no) {
        System.out.println("~~次");
        if (this.no==no) {
            return this;
        }
        RoleD temp=null;
        if (this.left!=null){
            temp= this.left.preSearch(no);
        }
        if (temp!=null) {
            return temp;
        }
        if (this.right!=null){
            temp= this.right.preSearch(no);
        }
        return temp;
    }
    public RoleD infixSearch(int no) {

        RoleD temp=null;
        if (this.left!=null){
            temp= this.left.infixSearch(no);
        }
        if (temp!=null) {
            return temp;
        }
        System.out.println("~~次");
        if (this.no==no) {
            return this;
        }
        if (this.right!=null){
            temp= this.right.infixSearch(no);
        }
        return temp;
    }
    public RoleD postSearch(int no) {

        RoleD temp=null;
        if (this.left!=null){
            temp= this.left.postSearch(no);
        }
        if (temp!=null) {
            return temp;
        }
        if (this.right!=null){
            temp= this.right.postSearch(no);
        }
        if (temp!=null) {
            return temp;
        }
        System.out.println("~~次");
        if (this.no==no) {
            return this;
        }
        return temp;
    }

    public void delete(int no) {
        if (this.left!=null&&this.left.no==no) {
            this.left=null;
            return;
        }
        if (this.right!=null&&this.right.no==no) {
            this.right=null;
            return;
        }
        if (this.left!=null){
            this.left.delete(no);
        }
        if (this.right!=null){
            this.right.delete(no);
        }
    }
}

//二叉树
class BinaryD{

    private RoleD root;

    public void setRoot(RoleD root) {
        this.root=root;
    }

    public void preOrder() {
        if (root!=null) {
            root.preOrder();
        }else {
            System.out.println("链表为空");
        }
    }
    public void infixOrder() {
        if (root!=null) {
            root.infixOrder();
        }else {
            System.out.println("链表为空");
        }
    }
    public void postOrder() {
        if (root!=null) {
            root.postOrder();
        }else {
            System.out.println("链表为空");
        }
    }
    public RoleD preSearch(int no) {
        if (root!=null) {
            return root.preSearch(no);
        }else {
            System.out.println("二叉树为空");
            return null;
        }
    }
    public RoleD infixSearch(int no) {
        if (root!=null) {
            return root.infixSearch(no);
        }else {
            System.out.println("二叉树为空");
            return null;
        }
    }
    public RoleD postSearch(int no) {
        if (root!=null) {
            return root.postSearch(no);
        }else {
            System.out.println("二叉树为空");
            return null;
        }
    }

    public void delete(int no) {
        if (root!=null) {
            if (root.getNo()==no) {
                root=null;
            }else {
                root.delete(no);
            }
        }else {
            System.out.println("二叉树为空");
        }
    }
}
